/* bcwti
*
* Copyright (c) 2013 Parametric Technology Corporation (PTC). All Rights
* Reserved.
*
* This software is the confidential and proprietary information of PTC
* and is subject to the terms of a software license agreement. You shall
* not disclose such confidential information and shall use it only in accordance
* with the terms of the license agreement.
*
* ecwti
*/

package com.jl.slick.run;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Vector;

import org.newdawn.slick.Animation;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

import com.jl.slick.objects.graphics.Level;


/**
 * GameObject
 * @author jlitten
 *
 * @Created: Apr 18, 2013
 * Uniontown Solution Center 
 *
 */

public class GameObject extends BasicGame
{

	int currentlvl;
	ArrayList<Level> levels;
	Point playerloc;
	SpriteSheet player;
	Animation playercurrent;
	boolean l_key, r_key;
	
	  /**
	   * Creates a GameObject to run, requiring a title and list of levels.
	   * @param title Title of the game
	   * @param levels An ArrayList of levels that are run in order, 
	   */
	public GameObject(String title, ArrayList<Level> levels)
	{
		super("Flip - " + title);
		this.currentlvl = 0;
		this.levels = new ArrayList<Level>(1);
		l_key = false;
		r_key = false;
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.Game#render(org.newdawn.slick.GameContainer, org.newdawn.slick.Graphics)
	 */
	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException
	{
		g.drawImage(this.levels.get(currentlvl).getWorld(), 0, 0);
		
		if(!(this.l_key && this.r_key))
		g.drawImage(this.playercurrent.getImage(1), playerloc.x, playerloc.y);
		if(this.r_key)
		g.drawAnimation(this.playercurrent, playerloc.x, playerloc.y);
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
	 */
	@Override
	public void init(GameContainer gc) throws SlickException
	{	
		Level in = Level.readIn("Test");
		Vector<Point> Collide = in.getCollide();
		in = new Level(in.getWorld(), new Point(1,2), new Point(6,10), "Test");
		in.setCollide(Collide);
		levels.add(in);
		this.playerloc = this.levels.get(currentlvl).getBegin();
		this.playerloc.x = playerloc.x*32;
		this.playerloc.y = playerloc.y*32;
		player = new SpriteSheet("resources/char.png", 32, 32);
		playercurrent = new Animation();
		for(int i = 0; i<2; i++)
			playercurrent.addFrame(player.getSubImage(i, 3), 100);
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer, int)
	 */
	@Override
	public void update(GameContainer gc, int g) throws SlickException
	{
		if(this.l_key)
			this.playerloc.move(playerloc.x-2, playerloc.y);
		if(this.r_key)
			this.playerloc.move(playerloc.x+2, playerloc.y);
		
		applyGravity();

	}

	@Override
	public void keyPressed(int key, char c) 
	{
		if(key == Input.KEY_LEFT)
			this.l_key = true;
		if(key == Input.KEY_RIGHT)
			this.r_key = true;
	}
	
	@Override
	public void keyReleased(int key, char c) 
	{
		if(key == Input.KEY_LEFT)
			this.l_key = false;
		if(key == Input.KEY_RIGHT)
			this.r_key = false;
	}
	
	private void applyGravity()
	{
		for(Point collide : this.levels.get(this.currentlvl).getCollide())
		{
			if(collide.x == playerloc.x/32)
			{
				System.out.println((this.playerloc.y+16) + "//" + ((collide.y*32)-32));
				if(this.playerloc.y+16 != (collide.y*32)-32)
				{
					playerloc.move(playerloc.x, playerloc.y+2);
				}
			}
			
		}
	}
	

	

}
